package com.sram.dao;

import com.sram.entity.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/3/16.
 */
@Service("userDao")
public class UserDao extends BaseDao{
    /*hql查询*/
    public List<User>  getUserByName(String name, String pwd) {
        Session session =  getCurrentSession();
        StringBuffer hql = new StringBuffer();
        hql.append("from User  where userName ='"+name+" 'and password='"+pwd +"'");
        Query query = session.createQuery(hql.toString());
        return query.list();
    }
    /*原生sql查询 2*/
    public List<User>  getUserByName2(String name, String pwd) {
        Session session =  getCurrentSession();
        StringBuffer sql = new StringBuffer();
        sql.append("select b.id,b.user_name userName,b.password,b.age,b.gender from h1_user b where b.user_name ='"+name+" 'and b.password='"+pwd +"'");
        Query query = session.createSQLQuery(sql.toString()).setResultTransformer(Transformers.aliasToBean(User.class));
//      Query query = session.createSQLQuery(sql.toString()).addEntity(User.class);
        return query.list();
    }
    /*原生sql查询 3*/
    public List getUserByName3(String name, String pwd) {
        Session session =  getCurrentSession();
        StringBuffer sql = new StringBuffer();
        sql.append("select b.id as id,b.user_name as name from h1_user b where b.user_name ='"+name+" 'and b.password='"+pwd +"'");
        Query query = session.createSQLQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return query.list();
    }
}
